package com.deng.maintenance.navigation.view.kpi
{
	import com.deng.common.model.CommonModelLocator;
	import com.deng.maintenance.model.MaintenanceModelLocator;
	import com.deng.maintenance.navigation.vo.KPIValueVO;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import mx.controls.Alert;
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.utils.StringUtil;

	public class KPIValueTextInput extends TextInput
	{
		//private var txt:int;
		private var pvo:KPIValueVO;
		public function KPIValueTextInput()
		{
			this.restrict = "0-9\.";
			this.addEventListener(KeyboardEvent.KEY_UP,updateData);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(FlexEvent.SHOW,showHandler);
			super();
			//txt = new Number(this.text);
//			this.addEventListener(MouseEvent.CLICK,insideTxt);

//			this.addEventListener(FocusEvent.FOCUS_IN,insideTxt);
//			
//			this.addEventListener(FocusEvent.FOCUS_OUT,updateData);
//			
//			this.addEventListener(FlexEvent.SHOW,showHandler);
//			this.addEventListener(KeyboardEvent.KEY_DOWN,updateData1);
//			super();
		}
		private function showHandler(event:FlexEvent):void{
			pvo = this.data as KPIValueVO;
			this.text = pvo.kpi_input_value;
			if(pvo.status=="1"){
				this.enabled = false;
			}else{
				this.enabled = true;
			}
		}
		private function insideTxt(event:Event):void{
			pvo = this.data as KPIValueVO;
			//txt = new Number(this.text);
		}
		
		public function initResult(event:ResultEvent) :void{
			CommonModelLocator.getInstance().progress.endRun();
			var obj:Object=event.result;
			if(obj!=null){
				pvo.kpi_score=obj+"";
				for(var i:int = 0;i<MaintenanceModelLocator.getInstance().kpivalues.length;i++){
					var vo:KPIValueVO = MaintenanceModelLocator.getInstance().kpivalues.getItemAt(i) as KPIValueVO;
					if(MaintenanceModelLocator.getInstance().kpivalues.getItemIndex(data)==i){
//						MaintenanceModelLocator.getInstance().kpivalues.setItemAt(pvo,i);
						vo.kpi_input_value=pvo.kpi_input_value;
						vo.kpi_score=pvo.kpi_score;
						return;
					}
				}
			}
		}
		
		private function faultResult(event:FaultEvent):void{
			CommonModelLocator.getInstance().progress.endRun();
			Alert.show(event.fault.getStackTrace());
		}
		public function updateData(event:KeyboardEvent):void{
			if(event.keyCode==13){
				CommonModelLocator.getInstance().progress.startRun(this);
				pvo.kpi_input_value = StringUtil.trim(this.text);
				var ro:RemoteObject = new RemoteObject();
				ro.destination = "service_KPTSettingImpl";
				ro.addEventListener(ResultEvent.RESULT,initResult);
				ro.addEventListener(FaultEvent.FAULT,faultResult);
				ro.count(pvo.kpi_id,pvo.kpi_input_value);
			}
			
			
//			
//			count();
//			this.focusEnabled=false;
		}
//		public function updateData1(event:KeyboardEvent):void{
//			if(event.keyCode==13){
//				count();
//			}
//		}
		
//		public function count():void{
//			if(this.text!=""){
//				if(pvo.status=="1"){
//					Alert.show("已经审核，无法修改！");
//					return ;
//				}
//				CommonModelLocator.getInstance().progress.startRun(this);
//				pvo.kpi_input_value = StringUtil.trim(this.text);
//				var ro:RemoteObject = new RemoteObject();
//				ro.destination = "service_KPTSettingImpl";
//				ro.addEventListener(ResultEvent.RESULT,initResult);
//				ro.addEventListener(FaultEvent.FAULT,faultResult);
//				
//				ro.count(pvo.kpi_id,pvo.kpi_input_value);
//				
//			}
//		}
	}
}